﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Collections;

namespace TextMIS
{
    public partial class frmRegister : Form
    {
        public frmRegister()
        {
            InitializeComponent();
        }

        string str_date = DateTime.Now.ToString();//体检日期
        string str_state = "Update";//新增还是保存
        string str_RingNo = "";//流水登记号
        string str_HospiName = "";//医院名称
        string str_HospTel = "";//医院电话
        string str_PEstate = "";//体检状态
        sysopr sysdic = new sysopr(); //系统字典获取数据
        registopr regopr = new registopr(); //注册相关信息
        dbopr baseinfo = new dbopr();//基础信息 
        DataTable dt_tj_tjjlb = null; 


        private void frmRegister_Load(object sender, EventArgs e)
        {
            lv_tjxm.SmallImageList = imageList1;
            lv_tjxm.StateImageList = imageList1;
            lv_tjxm.LargeImageList = imageList1;

            //if (!Program.sfzc)//没有注册
            //{
            //    if (tjdjbiz.GetTjCounts() > 300)
            //    {
            //        MessageBox.Show("软件试用期已到，请联系供应商！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //        Application.Exit();
            //    }
            //}
            dt_Regdate.Value = Convert.ToDateTime(str_date);
            dt_BirthDate.Value = sysdic.GetServerDate();
            str_HospiName = sysdic.GetXtCsz("TjDwMc");
            str_HospTel = sysdic.GetXtCsz("TjDwDh");

            //string str_mrsfkz = xtbiz.GetXtCsz("mrsfkzjg");//默认收费控制与否
            //if (str_mrsfkz == "1") //收费控制
            //{
            //    ckb_sfbz.Checked = true;
            //}
            //else                   //不默认收费控制
            //{
            //    ckb_sfbz.Checked = false;
            //}

            txt_tjdw.Tag = "9999";
            txt_tjdw.Text = "个人体检";

            ArrayList mylist = new ArrayList();
            mylist.Add(new DictionaryEntry("1", "套餐A"));
            mylist.Add(new DictionaryEntry("2", "套餐B"));
            mylist.Add(new DictionaryEntry("3", "套餐C"));
            cb_Package.DataSource = mylist;

            BindData();
        }

        #region 绑定数据
        void BindData()
        {
            cb_Sex.DataSource = sysdic.GetDicInfo(1);//性别
            cb_Sex.DisplayMember = "xmmc";
            cb_Sex.ValueMember = "bzdm";

            cb_Merriage.DataSource = sysdic.GetDicInfo(12);//婚姻
            cb_Merriage.DisplayMember = "xmmc";
            cb_Merriage.ValueMember = "bzdm";
            cb_Merriage.SelectedIndex = -1;

            cb_Nation.DataSource = sysdic.GetDicInfo(2);//民族
            cb_Nation.DisplayMember = "xmmc";
            cb_Nation.ValueMember = "bzdm";
            cb_Nation.SelectedValue = "1";

            cb_PersonTyp.DataSource = sysdic.GetDicInfo(8);//人员类别
            cb_PersonTyp.DisplayMember = "xmmc";
            cb_PersonTyp.ValueMember = "bzdm";
            cb_PersonTyp.SelectedIndex = -1;

            cb_PETyp.DataSource = sysdic.GetDicInfo(10);//体检业务-体检类型（岗前 学前 离岗 随访 应急）
            cb_PETyp.DisplayMember = "xmmc";
            cb_PETyp.ValueMember = "bzdm";
            cb_PETyp.SelectedIndex = 0;

            cb_Package.DataSource = baseinfo.Get_PackList();//套餐
            cb_Package.DisplayMember = "Pack_name";
            cb_Package.ValueMember = "Pack_id";
            cb_Package.SelectedIndex = -1;

            cb_EduDegr.DataSource = sysdic.GetDicInfo(18);//文化程度
            cb_EduDegr.DisplayMember = "xmmc";
            cb_EduDegr.ValueMember = "bzdm";
            cb_EduDegr.SelectedIndex = -1;


        }
        #endregion
        #region 清空
        void ClearControl()
        {
            tb_RegId.Text = "";
            tb_Count.Text = "1";
            tb_Name.Text = "";
            tb_Age.Text = "";
            cmb_hy.SelectedIndex = -1;
            txt_sykh.Text = "";
            cmb_mz.SelectedValue = "1";
            txt_sfzh.Text = "";
            cmb_rylx.SelectedIndex = -1;
            //txt_phone.Text = "";
            txt_mobile.Text = "";
            cmb_ywlx.SelectedValue = "04";//*********************************
            lv_tjxm.Items.Clear();
            //dt_tj_tjjlb = null;
            //cbx_sszl.SelectedIndex = -1;
            //cbx_hy.SelectedIndex = -1;            
            //cmbGz.SelectedIndex = -1;
            //txt_jhgl.Text = "";
        }
        #endregion

        private void btn_add_Click(object sender, EventArgs e)
        {
            ClearControl();
            str_state = "Insert";
            //tb_RegId.Text = "";
            str_RingNo = "";
            str_PEstate = "";
            tb_Name.ReadOnly = false;
            this.ActiveControl = tb_Name;

            //必须处理图像缓存，否则要出错
            //if (pb_photo.Image != null) pb_photo.Image.Dispose();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            if (tb_RegId.Text.Trim() == "") return;
            DialogResult result = MessageBox.Show("您将删除编号为【" + tb_RegId.Text.Trim() + "】的登记信息,是否继续？", "删除登记信息",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (result == DialogResult.No) return;

            string str_delete ="1";//0不做任何限制，可以删除；1录入了体检数据后不允许删除；2总检后不允许删除
            int str_statu = regopr.Get_Statu(tb_RegId.Text.Trim(), tb_Count.Text.Trim());//0新录入 1录入数据了 2总检了
            if (str_delete == "1" && str_statu > 0)
            {
                MessageBox.Show("已经录入了资料的人员不允许删除！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (str_delete == "2" && str_statu > 1)
            {
                MessageBox.Show("已经总检的人员不允许删除！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            regopr.Delete_Reg(tb_RegId.Text.Trim(), tb_Count.Text.Trim());
            MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            DG_Reg.Rows.Remove(DG_Reg.CurrentRow);
            ClearControl();
        }
        //单位选项
        private void rb_dw_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_dw.Checked)
            {
                bt_tjdw.Enabled = true;
                txt_tjdw.Tag = "";
                txt_tjdw.Text = "";
            }
        }
        //个人选项
        private void rb_gr_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_gr.Checked)
            {
                bt_tjdw.Enabled = false;
                txt_tjdw.Tag = "9999";
                txt_tjdw.Text = "个人体检";

                cb_Package.DataSource = baseinfo.Get_PackList();
                cb_Package.DisplayMember = "mc";
                cb_Package.ValueMember = "bh";
                cb_Package.SelectedIndex = -1;
            }
        }
        //体检单位-需要做
        //private void bt_tjdw_Click(object sender, EventArgs e)
        //{
        //    Form_tjdw frm = new Form_tjdw();
        //    if (frm.ShowDialog() == DialogResult.OK)
        //    {
        //        txt_tjdw.Text = frm.str_tjdwmc;
        //        txt_tjdw.Tag = frm.str_tjdwid;

        //        cb_Package.DataSource = tjdjbiz.Get_TJ_TC_FZ(frm.str_tjdwid.Substring(0, 4));
        //        cb_Package.DisplayMember = "mc";
        //        cb_Package.ValueMember = "bh";
        //        cb_Package.SelectedIndex = -1;
        //    }
        //}

        private void bt_save_Click(object sender, EventArgs e)
        {
            #region 输入判断
            if (object.Equals(null, cb_Package.SelectedValue))
            {
                MessageBox.Show("请选择套餐信息！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = cb_Package;
                return;
            }
            if (txt_tjdw.Text.Trim() == "")
            {
                MessageBox.Show("请选择体检单位！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = bt_tjdw;
                return;
            }
            if (tb_Name.Text.Trim() == "")
            {
                MessageBox.Show("请填写姓名！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = tb_Name;
                return;
            }
            if (object.Equals(null, cb_Sex.SelectedValue))
            {
                MessageBox.Show("请选择性别！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = cb_Sex;
                return;
            }
            if (tb_Age.Text.Trim() == "")
            {
                MessageBox.Show("请填写年龄！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = tb_Age;
                return;
            }
            if (object.Equals(null, cb_PETyp.SelectedValue))
            {
                MessageBox.Show("请选择体检类型！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = cb_PETyp;
                return;
            }
            #endregion

            #region 收费判断
            //string str_sfkz = xtbiz.GetXtCsz("sfbkzts");//取消收费时是否提示
            //if (ckb_sfbz.Checked == true)   //要收费控制
            //{
            //    str_sfbz = "1";
            //}
            //else
            //{
            //    if (str_sfkz == "1")  //进行收费控制要提示
            //    {
            //        DialogResult dlg = MessageBox.Show("该体检者不进行收费流程控制吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            //        if (dlg == DialogResult.Yes)
            //        {
            //            str_sfbz = "0";
            //        }
            //        else
            //        {
            //            ckb_sfbz.Focus();
            //            return;
            //        }
            //    }
            //    else
            //    {
            //        str_sfbz = "0";  //
            //    }
            //}
            #endregion


            #region 取体检编号和流水号
            string number = "";
            if (tb_RegId.Text.Trim() == "")
            {
                number = sysdic.GetId("PE_RegID", 1);
                //体检编号获取到最大值后根据格式
                //int len = number.Length;
                //for (int i = 0; i < 5 - len; i++)
                //{
                //    number = "0" + number;
                //}
                //tb_RegId.Text = System.DateTime.Today.ToString("yyyy") + number;
                tb_RegId.Text = number;

            }


            if (str_RingNo == "")
            {
                //if (sysdic.GetXtCsz("djlshgz") == "2")  
                //{
                    str_RingNo = sysdic.GetId("PE_RingID", 1);//体检流水号规则YYMMDD+5位
                //}
                //else
                //{
                //    str_RingNo = tjdjbiz.Get_proc_get_djlsh(str_tjrq);
                //}
            }
            #endregion

            //if (image == null)
            //{
            //    string path = Application.StartupPath + @"\img\空白头像.jpg";
            //    FileStream fs;
            //    using (fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            //    {
            //        BinaryReader br = new BinaryReader(fs);
            //        image = br.ReadBytes((int)fs.Length);
            //    }
            //    //Image img = Image.FromFile();
            //}



            string str_Merr = "";
            if (object.Equals(null, cb_Merriage.SelectedValue)) str_Merr = ""; else str_Merr = cb_Merriage.SelectedValue.ToString().Trim();
            string str_Nation = "";
            if (object.Equals(null, cb_Nation.SelectedValue)) str_Nation = ""; else str_Nation = cb_Nation.SelectedValue.ToString().Trim();
            string str_PersonTyp = "";
            if (object.Equals(null, cb_PersonTyp.SelectedValue)) str_PersonTyp = ""; else str_PersonTyp = cb_PersonTyp.SelectedValue.ToString().Trim();
            string str_PETyp = "";
            if (object.Equals(null, cb_PETyp.SelectedValue)) str_PETyp = ""; else str_PETyp = cb_PETyp.SelectedValue.ToString().Trim();



            //string str_dwbh = txt_tjdw.Tag.ToString().Trim();
            string str_dwbh = "9999";
            if (str_dwbh.Length > 4) str_dwbh = str_dwbh.Substring(0, 4);//单位编号
            //string str_bmbh = txt_tjdw.Tag.ToString().Trim();
            //if (str_bmbh.Length == 4) str_bmbh = "";
            

            string EduDegr = "";
            if (cb_EduDegr.SelectedIndex != -1)
            {
                EduDegr = cb_EduDegr.SelectedValue.ToString();
            }
            string str_PackageID = cb_Package.SelectedValue.ToString().Trim();//套餐ID 或者分组编号
            if (str_state == "Insert")
            {
                //tjdjBiz tjdjbiz1 = new tjdjBiz();
                str_PEstate = "0"; //新登记体检状态为零
                regopr.str_Insert_PEReg(tb_RegId.Text.Trim(), str_RingNo, tb_Count.Text.Trim(), str_date, sysdic.GetServerDate().ToString(), tb_Name.Text.Trim(),
                    cb_Sex.SelectedValue.ToString().Trim(), dt_BirthDate.Value.ToString().Trim(), tb_Age.Text.Trim(), str_Merr,
                    tb_index.Text.Trim(), str_Nation, tb_IDCardNum .Text.Trim(), str_PersonTyp,
                    tb_Tel.Text.Trim(),"0", str_PETyp, 
                    str_dwbh, str_PackageID, str_PEstate, Program.userid, "1", EduDegr);

                DataTable dt = null;
                //分组或套餐项目
                //if (str_PackageID.Length == 6)//单位分组项目************************************
                //{
                //    dt = regopr.Get_tj_dwfz_dt(str_PackageID);
                //}
                //else//套餐项目
                //{
                //    dt = baseinfo.Get_tj_tc_dt(str_PackageID);
                //}
                //体检记录表根据套餐项建立相应记录--需要完善
                //foreach (DataRow dr in dt.Rows)
                //{
                //    string str_xb = cmb_xb.SelectedValue.ToString().Trim();
                //    if (regopr.CheckSex(dr["zhxm"].ToString().Trim(), str_xb) == 0)
                //    {
                //        MessageBox.Show("所选择的项目【编号：" + dr["zhxm"].ToString().Trim() + "】存在与性别不匹配，或者组合项目明细为空，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //        return;
                //    }
                //    DataTable dt_tj_zhxm_hd = regopr.Get_tj_zhxm_hd(dr["zhxm"].ToString().Trim());
                //    string str_xh = sysdic.GetId("tjjlbxh", 1);//体检记录本序号
                //    string str_lxbh = dt_tj_zhxm_hd.Rows[0]["tjlx"].ToString().Trim();
                //    string str_tjxmbh = dt_tj_zhxm_hd.Rows[0]["bh"].ToString().Trim();
                //    string str_xmdj = dt_tj_zhxm_hd.Rows[0]["dj"].ToString().Trim();
                //    string str_zxks = dt_tj_zhxm_hd.Rows[0]["tjlx"].ToString().Trim();
                //    string str_xmlx = dt_tj_zhxm_hd.Rows[0]["jcjylx"].ToString().Trim();
                //    string str_sflb = dt_tj_zhxm_hd.Rows[0]["sflb"].ToString().Trim();
                //    regopr.str_Insert_tj_tjjlb(str_xh, txt_tjbh.Text.Trim(), txt_tjcs.Text.Trim(), str_lxbh, str_date, str_tjxmbh, str_xmdj, "0", "1", str_sflb, str_zxks, str_xmlx);
                //}

              

                regopr.Exec_ArryList();//登记表执行成功后在执行记录表
            }
            else//该处修改不调整记录表项目信息
            {
                //regopr.Update_TJ_TJDJB(txt_tjbh.Text.Trim(), txt_tjcs.Text.Trim(), str_tjrq, txt_xm.Text.Trim(), cmb_xb.SelectedValue.ToString().Trim(),
                //    dtp_csrq.Value.ToString().Trim(), txt_nl.Text.Trim(), str_hyzk, txt_sykh.Text.Trim(), str_mz, txt_sfzh.Text.Trim(), str_rylx,
                //    "", txt_mobile.Text.Trim(), "", str_dwbh, str_bmbh);

                //if (str_ywlx.Trim() == "01")  //职业体检保存职业病人员信息
                //{
                //    tjdjbiz.Update_TJ_ZYB_RYXX(txt_tjbh.Text.Trim(), txt_tjcs.Text.Trim(), txt_xm.Text.Trim(), cmb_xb.SelectedValue.ToString().Trim(), txt_sfzh.Text.Trim(), dtp_csrq.Value.ToString().Trim(), txt_tjdw.Text.Trim(), txt_mobile.Text.Trim(), "", "", str_tjrq, str_rylx,
                //   "", "", str_tjrq, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", gz, "", "", "", str_mz, str_hyzk, txt_zgl.Text.Trim(), txt_jhgl.Text.Trim(), str_whys, "");
                //}
            }
            MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (ck_sqd.Checked && str_state == "Insert")//打印申请单
            {
                //PrintRdlc_Sqd(txt_tjbh.Text.Trim(), txt_tjcs.Text.Trim(), str_djlsh);
            }
            if (ck_zyd.Checked && str_state == "Insert")//打印指引单
            {
                //PrintRdlc_Zyd(txt_tjbh.Text.Trim(), txt_tjcs.Text.Trim(), str_djlsh);
            }
            DG_Reg.DataSource = regopr.Get_Reginfo(dt_Regdate.Value.ToString("yyyy-MM-dd"));
            //dgv_tjdjb.Rows[dgv_tjdjb.Rows.Count].Selected = true;
            //lv_tjxm.Items.Clear();

            //体检记录表 --需完善
            //dt_tj_tjjlb = regopr.Get_dt_tj_tjjlb(txt_tjbh.Text.Trim(), txt_tjcs.Text.Trim());
            //foreach (DataRow dr in dt_tj_tjjlb.Rows)
            //{
            //    ListViewItem item = new ListViewItem();
            //    item.Tag = dr["zhxm"].ToString().Trim();
            //    item.Text = dr["mc"].ToString().Trim();
            //    item.ImageIndex = 4;
            //    lv_tjxm.Items.Add(item);
            //}
            str_state = "Update";
            str_RingNo = "";//登记流水号
            //bt_add_Click(null, null); //add
        }

        //打印
        //void PrintRdlc_Sqd(string tjbh, string tjcs, string djlsh)
        //{
        //    BarcodeControl barcode = new BarcodeControl();
        //    barcode.BarcodeType = BarcodeType.CODE128C;
        //    barcode.Data = djlsh;
        //    barcode.CopyRight = "";
        //    MemoryStream stream = new MemoryStream();
        //    barcode.MakeImage(ImageFormat.Png, 1, 50, true, false, null, stream);
        //    Bitmap myimge = new Bitmap(stream);

        //    string str_path = Application.StartupPath + @"/barcode.png";
        //    myimge.Save(str_path, ImageFormat.Png);
        //    str_path = "file:///" + str_path;

        //    LocalReport report = new LocalReport();

        //    DataTable dt1 = rdlcbiz.Get_tj_sqdlx_hd(tjbh, tjcs);
        //    DataTable dt2 = rdlcbiz.Get_v_tj_tjdjb(tjbh, tjcs);
        //    report.ReportPath = Application.StartupPath + @"/rdlcreport/Report_tjsqd.rdlc";
        //    report.EnableExternalImages = true;
        //    ReportParameter rp1 = new ReportParameter("tjdw", str_tjdw);
        //    ReportParameter rp2 = new ReportParameter("barcode", str_path);
        //    report.DataSources.Clear();
        //    report.SetParameters(new ReportParameter[] { rp1, rp2 });
        //    report.DataSources.Add(new ReportDataSource("PEISDataSet_tj_sqdlx_hd", dt1));
        //    report.DataSources.Add(new ReportDataSource("PEISDataSet_v_tj_tjdjb", dt2));

        //    RdlcPrintNew rdlcprint = new RdlcPrintNew();
        //    rdlcprint.Run(report, "体检申请单打印", false, "A4");
        //}

        //void PrintRdlc_Zyd(string tjbh, string tjcs, string djlsh)
        //{
        //    BarcodeControl barcode = new BarcodeControl();
        //    barcode.BarcodeType = BarcodeType.CODE128C;
        //    barcode.Data = djlsh;
        //    barcode.CopyRight = "";
        //    MemoryStream stream = new MemoryStream();
        //    barcode.MakeImage(ImageFormat.Png, 1, 50, true, false, null, stream);
        //    Bitmap myimge = new Bitmap(stream);

        //    string str_path = Application.StartupPath + @"/barcode.png";
        //    myimge.Save(str_path, ImageFormat.Png);
        //    str_path = "file:///" + str_path;
        //    myimge.Dispose();     //201203
        //    string strLog = "file:///" + Application.StartupPath + @"/Img/log.jpg";

        //    DataTable dt1 = rdlcbiz.Get_tj_tjjlb(tjbh, tjcs);
        //    DataTable dt2 = rdlcbiz.Get_v_tj_tjdjb(tjbh, tjcs);
        //    DataTable dt3 = rdlcbiz.Get_v_tj_fyxx(tjbh, tjcs);

        //    LocalReport report = new LocalReport();
        //    report.ReportPath = Application.StartupPath + @"/rdlcreport/Report_tjzyd.rdlc";
        //    report.EnableExternalImages = true;
        //    ReportParameter rp1 = new ReportParameter("tjdw", Program.reg_dwmc);
        //    ReportParameter rp2 = new ReportParameter("barcode", str_path);
        //    ReportParameter rp3 = new ReportParameter("tjdh", str_dwdh);
        //    ReportParameter rp4 = new ReportParameter("log", strLog);
        //    report.DataSources.Clear();
        //    report.SetParameters(new ReportParameter[] { rp1, rp2, rp3, rp4 });
        //    report.DataSources.Add(new ReportDataSource("PEISDataSet_tj_tjjlb", dt1));
        //    report.DataSources.Add(new ReportDataSource("PEISDataSet_v_tj_tjdjb", dt2));
        //    report.DataSources.Add(new ReportDataSource("PEISDataSet_v_tj_fyxx", dt3));
        //    RdlcPrintNew rdlcprint = new RdlcPrintNew();
        //    rdlcprint.Run(report, "体检指引单打印", false, "A4");
        //}

        private void dt_BirthDate_Leave(object sender, EventArgs e)
        {
            try
            {
                tb_Age.Text = Convert.ToString(sysdic.GetServerDate().Year - dtp_csrq.Value.Year);
            }
            catch
            {

            }
        }

        //private void tb_Age_Leave(object sender, EventArgs e)
        //{
        //    Common.Common comn = new Common.Common();
        //    txt_nl.Text = comn.CharConverter(txt_nl.Text.Trim());
        //    string nl = txt_nl.Text.Trim();
        //    if (nl != "")
        //    {
        //        if (comn.Szyz(nl) != -1)
        //        {
        //            if (Convert.ToInt32(nl) > 100 || Convert.ToInt32(nl) < 0)
        //            {
        //                MessageBox.Show("年龄必须在【0-100】之间！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //                this.ActiveControl = tb_Age;
        //                tb_Age.SelectAll();
        //                return;
        //            }
        //        }
        //    }
        //    try
        //    {
        //        dtp_csrq.Value = sysdic.GetServerDate();
        //        dtp_csrq.Value = dtp_csrq.Value.AddYears(-Convert.ToInt32(txt_nl.Text.Trim()));
        //    }
        //    catch
        //    {
        //    }
        //}

        //体检日期改变 体检信息栏相关信息改变。
        private void dt_Regdate_ValueChanged(object sender, EventArgs e)
        {
            DG_Reg.DataSource = regopr.Get_Reginfo(dt_Regdate.Value.ToString("yyyy-MM-dd"));
            str_date = dt_Regdate.Value.ToString("yyyy-MM-dd");
            //体检项目
            //lv_tjxm.Items.Clear();
        }

        //体检登记选中
        private void DG_Reg_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            str_state = "Update";
            DataGridViewRow dgr = DG_Reg.SelectedRows[0];
            //要根据 regid 和 count 检索 到相关条目
            string str_id = dgr.Cells["regid"].Value.ToString().Trim();
            string str_cnt = dgr.Cells["count"].Value.ToString().Trim();

            DataTable dt = regopr.Get_PERegInfo(str_id, str_cnt);
            if (dt.Rows.Count < 1) return;
            //RingNum,Reg_id,Count,PE_date,Reg_Name,Reg_Sex,Reg_Birth,Reg_Age
            cb_EduDegr.SelectedValue = dt.Rows[0]["EduDegr"].ToString().Trim();
            str_PEstate = dt.Rows[0]["PE_stau"].ToString().Trim();//体检状态
            tb_RegId.Text = dt.Rows[0]["Reg_id"].ToString().Trim();
            tb_Count.Text = dt.Rows[0]["Count"].ToString().Trim(); 
            dt_Regdate.Value = Convert.ToDateTime(dt.Rows[0]["PE_date"].ToString().Trim());
            tb_Name.Text = dt.Rows[0]["Reg_Name"].ToString().Trim();
            //txt_zgl.Text = dt.Rows[0]["gl"].ToString().Trim();
            //cmbGz.SelectedValue = dt.Rows[0]["gz"].ToString().Trim();
            //cmb_whys.SelectedValue = dt.Rows[0]["whys"].ToString().Trim();
            dt_BirthDate.Value = Convert.ToDateTime(dt.Rows[0]["Reg_Birth"].ToString().Trim());            
            cb_Sex.SelectedValue = dt.Rows[0]["Reg_Sex"].ToString().Trim();
            tb_Age.Text = dt.Rows[0]["Reg_Age"].ToString().Trim();
            cb_Merriage.SelectedValue = dt.Rows[0]["Merriage"].ToString().Trim();
            //txt_sykh.Text = dt.Rows[0]["sykh"].ToString().Trim();
            cb_Nation.SelectedValue = dt.Rows[0]["Reg_Nation"].ToString().Trim();
            string cardNo = dt.Rows[0]["IDCardNum"].ToString().Trim();
            //身份证号月日加密处理
            //if (cardNo != "")  //510132198010076618    511204761018072
            //{
            //    if (cardNo.Length == 18)
            //    {
            //        cardNo = cardNo.Substring(0, 8) + "******" + cardNo.Substring(14, 4);
            //    }
            //    else   //15位
            //    {
            //        cardNo = cardNo.Substring(0, 6) + "******" + cardNo.Substring(12, 3);
            //    }
            //}
            tb_IDCardNum.Text = cardNo;
            cb_PersonTyp.SelectedValue = dt.Rows[0]["PersonTyp"].ToString().Trim();
            //txt_phone.Text = dt.Rows[0]["phone"].ToString().Trim();
            tb_Tel.Text = dt.Rows[0]["mobile"].ToString().Trim();
            //txt_lxdz.Text = dt.Rows[0]["address"].ToString().Trim();
            cb_PETyp.SelectedValue = dt.Rows[0]["PE_Type"].ToString().Trim();
            //string sfbz = dt.Rows[0]["sfbz"].ToString().Trim();
            //if (sfbz == "1") //收费
            //{
            //    ckb_sfbz.Checked = true;
            //}
            //else     //0
            //{
            //    ckb_sfbz.Checked = false;
            //}
            //cbx_hy.Text = dt.Rows[0]["bzhy"].ToString().Trim();
            //cbx_sszl.Text = dt.Rows[0]["sszl"].ToString().Trim();
            //txt_jhgl.Text = dt.Rows[0]["jhgl"].ToString().Trim();

            str_RingNo = dt.Rows[0]["RingNum"].ToString().Trim();//登记流水号，修改的时候，不需要产生登记流水号

            //部门体检-单位体检-个人体检
            //string str_bmbh = dt.Rows[0]["bmbh"].ToString().Trim();//部门编号
            //if (str_bmbh == "") str_bmbh = dt.Rows[0]["dwbh"].ToString().Trim();//单位编号
            //if (str_bmbh == "9999")
            //{
            //    rb_gr.Checked = true;
            //    txt_tjdw.Text = "个人体检";
            //}
            //if (str_bmbh.Length == 4 && str_bmbh != "9999")
            //{
            //    rb_dw.Checked = true;
            //    txt_tjdw.Text = tjdjbiz.Get_TJ_DW(str_bmbh);
            //}
            //if (str_bmbh.Length == 8)
            //{
            //    rb_dw.Checked = true;
            //    txt_tjdw.Text = tjdjbiz.Get_TJ_DW(str_bmbh.Substring(0, 4)) + "/" + tjdjbiz.Get_TJ_DW(str_bmbh);
            //}
            //if (str_bmbh.Length == 12)
            //{
            //    rb_dw.Checked = true;
            //    txt_tjdw.Text = tjdjbiz.Get_TJ_DW(str_bmbh.Substring(0, 4)) + "/" + tjdjbiz.Get_TJ_DW(str_bmbh.Substring(0, 8)) + "/" + tjdjbiz.Get_TJ_DW(str_bmbh);
            //}
            //txt_tjdw.Tag = str_bmbh;

            //套餐信息显示--待做
            //string str_PackageID = dt.Rows[0]["PackageID"].ToString().Trim();//套餐ID-或者分组ID
            //cb_Package.DataSource = regopr.Get_Package(str_bmbh.Substring(0, 4));
            //cb_Package.DisplayMember = "mc";
            //cb_Package.ValueMember = "bh";
            //cb_Package.SelectedValue = str_PackageID;

            //头像处理---------------------------------------------------------------------------------------------------------

            //try
            //{
            //    //MemoryStream buf = new MemoryStream((byte[])dt.Rows[0]["picture"],true);
            //    //Image showimage = Image.FromStream(buf, true);
            //    //pb_photo.Image = showimage;
            //    byte[] b = (byte[])dt.Rows[0]["picture"];

            //    MemoryStream buf = new MemoryStream(b, true);
            //    buf.Write(b, 0, b.Length);
            //    //Image showimage = Image.FromStream(buf, true);
            //    pb_photo.Image = new Bitmap(buf);
            //    buf.Dispose();//add
            //    buf.Close();

            //}
            //catch
            //{
            //    pb_photo.Image = null;
            //}
            //MemoryStream stream = new MemoryStream(b, true);
            //stream.Write(b, 0, b.Length);
            //pictureBox1.Image = new Bitmap(stream);
            //stream.Close();

            //FileStream fs = File.OpenRead(t_photo.Text);
            //byte[] imageb = new byte[fs.Length];
            //fs.Read(imageb, 0, imageb.Length);
            //fs.Close();

            //---------------------套餐包含 项目信息 -待做--------------------------------------------------
            //lv_tjxm.Items.Clear();
            //var dt_tj_tjjlb = tjdjbiz.Get_dt_tj_tjjlb(str_tjbh, str_tjcs);
            //foreach (DataRow dr in dt_tj_tjjlb.Rows)
            //{
            //    ListViewItem item = new ListViewItem();
            //    item.Tag = dr["zhxm"].ToString().Trim();
            //    item.Text = dr["mc"].ToString().Trim();
            //    item.ImageIndex = 6;
            //    lv_tjxm.Items.Add(item);
            //}

            //加载费用信息
            //LoadDgvFyxx(str_tjbh, str_tjcs);
        }

        //费用信息
        //void LoadDgvFyxx(string tjbh, string tjcs)
        //{
        //    DataTable dtFyxx = new DataTable();
        //    dtFyxx = tjdjbiz.GetTjfyxx(tjbh, tjcs);
        //    dgvFyxx.DataSource = dtFyxx;
        //    Calc();
        //}
        //费用相关计算
        //void Calc()
        //{
        //    string dj;
        //    decimal hj = 0;
        //    for (int i = 0; i < dgvFyxx.Rows.Count; i++)
        //    {
        //        dj = dgvFyxx.Rows[i].Cells["hj"].Value.ToString().Trim();
        //        if (dj == "") dj = "0";
        //        hj += Convert.ToDecimal(dj);
        //    }

        //    lblHj.Text = "合计金额：" + hj.ToString("0.00");

        //}

        private void bt_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 增加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (txt_tjbh.Text.Trim() == "") return;//没有保存不允许录入体检项目
            if (object.Equals(dt_tj_tjjlb, null)) return;
            if (str_PEstate == "2")
            {
                MessageBox.Show("该人员已经总检，不允许修改体检项目！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //另外一个窗体项目
            //Form_zhxmlr frm = new Form_zhxmlr(dt_tj_tjjlb, txt_tjdw.Tag.ToString().Trim(), txt_tjbh.Text.Trim(), txt_tjcs.Text.Trim(), str_tjrq, cmb_xb.SelectedValue.ToString().Trim());
            //if (frm.ShowDialog() == DialogResult.OK)
            //{
            //    lv_tjxm.Items.Clear();
            //    lv_tjxm.View = View.SmallIcon;
            //    dt_tj_tjjlb = frm.dt_tj_tc_dt;
            //    foreach (DataRow dr in dt_tj_tjjlb.Rows)
            //    {
            //        ListViewItem item = new ListViewItem();
            //        item.Tag = dr["zhxm"].ToString().Trim();
            //        item.Text = dr["mc"].ToString().Trim();
            //        item.ImageIndex = 6;
            //        lv_tjxm.Items.Add(item);
            //    }
            //}
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (txt_tjbh.Text.Trim() == "" || txt_tjcs.Text.Trim() == "") return;
            if (str_PEstate == "2")
            {
                MessageBox.Show("该人员已经总检，不允许修改体检项目！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try
            {
                regopr.Delete_tj_tjjlb(txt_tjbh.Text.Trim(), txt_tjcs.Text.Trim(), lv_tjxm.SelectedItems[0].Tag.ToString().Trim());
                //刷新
                lv_tjxm.Items.Clear();
                dt_tj_tjjlb = regopr.Get_dt_tj_tjjlb(txt_tjbh.Text.Trim(), txt_tjcs.Text.Trim());
                foreach (DataRow dr in dt_tj_tjjlb.Rows)
                {
                    ListViewItem item = new ListViewItem();
                    item.Tag = dr["zhxm"].ToString().Trim();
                    item.Text = dr["mc"].ToString().Trim();
                    item.ImageIndex = 6;
                    lv_tjxm.Items.Add(item);
                }
            }
            catch
            {

            }
        }

        private void txt_xm_Leave(object sender, EventArgs e)
        {
            if (str_state == "Update") return;
            if (txt_xm.Text.Trim() == "") return;
            DataTable dt_tjdjb = regopr.Get_TJ_TJDJB_XM(txt_xm.Text.Trim());
            if (dt_tjdjb.Rows.Count > 0)
            {
                //Form_tmqr frm = new Form_tmqr(dt_tjdjb);
                //if (frm.ShowDialog() == DialogResult.OK)
                //{
                //    string str_retrun = frm.str_return;
                //    if (str_retrun == "2")
                //    {
                //        txt_tjbh.Text = frm.str_tjbh;
                //        txt_tjcs.Text = Convert.ToString(Convert.ToInt32(frm.str_tjcs) + 1);
                //    }
                //}
            }
        }

        private void txt_sfzh_Leave(object sender, EventArgs e)
        {
            if (txt_sfzh.Text.Trim() == "") return;

            if (!CheckIDCard.CheckIDCardLenth(tb_IDCardNum.Text.Trim()))
            {
                MessageBox.Show("身份证号格式不正确，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ActiveControl = txt_sfzh;
                return;
            }
            //身份证计算年龄  510132198010076618    511204761018072
            string sfzh = txt_sfzh.Text.Trim();
            try
            {
                dtp_csrq.Value = Convert.ToDateTime(CheckIDCard.GetBrithdayFromIdCard(txt_sfzh.Text.Trim()));
                cmb_xb.Text = CheckIDCard.GetSexFromIdCard(txt_sfzh.Text.Trim());
                tb_Age.Text = regopr.GetNl(sfzh).ToString();
            }
            catch { }

            if (str_state == "Update") return;
            DataTable dt_tjdjb = regopr.Get_TJ_TJDJB_SFZH(txt_sfzh.Text.Trim());
            if (dt_tjdjb.Rows.Count > 0)
            {
                //Form_tmqr frm = new Form_tmqr(dt_tjdjb);
                //if (frm.ShowDialog() == DialogResult.OK)
                //{
                //    string str_retrun = frm.str_return;
                //    if (str_retrun == "2")
                //    {
                //        txt_tjbh.Text = frm.str_tjbh;
                //        txt_tjcs.Text = Convert.ToString(Convert.ToInt32(frm.str_tjcs) + 1);
                //    }
                //}
            }
        }

         
        //List<Tjjlb> GetTjjls(string tjbh, string tjcs, string sjh)
        //{
        //    List<Tjjlb> tjjls = new List<Tjjlb>();
        //    Tjjlb tjjl;
        //    for (int i = 0; i < dgvFyxx.Rows.Count; i++)
        //    {
        //        string sfbz = dgvFyxx.Rows[i].Cells["CHARGED"].Value.ToString().Trim();
        //        if (sfbz == "0")
        //        {
        //            tjjl = new Tjjlb();
        //            tjjl.Jsr = Program.userid;
        //            tjjl.Jsrq = xtbiz.GetDataNow().ToString();
        //            tjjl.Sjh = sjh;
        //            tjjl.Tjbh = tjbh;
        //            tjjl.Tjcs = tjcs;
        //            tjjl.Tjxmbh = dgvFyxx.Rows[i].Cells["zhxm"].Value.ToString().Trim();
        //            tjjls.Add(tjjl);
        //        }
        //    }

        //    return tjjls;
        //}



      


    }
}
